package com.tsingteng.cosfun.ui.search;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.SearchUserResp;
import com.tsingteng.cosfun.common.Constant;
import com.tsingteng.cosfun.ui.mine.HomeMineFragment;
import com.tsingteng.cosfun.ui.mine.TerminalActivity;
import com.tsingteng.cosfun.utils.ImageUtils;
import com.tsingteng.cosfun.utils.StringUtils;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;


public class SearchAdapter extends RecyclerView.Adapter{

    public enum FooterState {
        Normal, Loading, TheEnd;
    }
    private final int FOOTERLAYOUT = 100;//加载更多
    private List<SearchUserResp> listSearch;
    private Context context;
    private FocusSearchListener listener;
    public FooterHolder mFooterHolder;
    public SearchAdapter(Context mContext,List<SearchUserResp> mListSearch,FocusSearchListener mListener){
        this.context = mContext;
        this.listSearch = mListSearch;
        this.listener = mListener;

    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == 0) {
//            if ("init".equals(init)) {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_search_layout, parent, false);
//            }else{
//                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_fans_search,parent,false);
//            }
            return new ViewHolder(view);
        } else {//加载更多
            view = LayoutInflater.from(context).inflate(R.layout.listview_footer, parent, false);
            mFooterHolder = new FooterHolder(view);
            return mFooterHolder;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//        if ("init".equals(init)) {
            if (holder instanceof ViewHolder) {
                ((ViewHolder) holder).setData(position);
            }
//        }else {
//            if (holder instanceof ViewHolderFan) {
//                ((ViewHolderFan) holder).setDataFan(position);
//            }
//        }
    }

    @Override
    public int getItemCount() {
//        if ("init".equals(init)){
            if (listSearch.size()%4 == 0) {
                return listSearch.size()/4;
            }else{
                return listSearch.size()/4+1;
            }
//        }else {
//            if (listSearch == null) {
//                return 0;
//            }
//            return listSearch.size() + 1;
//        }
    }
//    public void setInit(String type){
//        this.init = type;
//    }
     public SearchUserResp getItem(int i) {
        return listSearch.get(i);
      }
    @Override
    public int getItemViewType(int position) {
        if (getItemCount() > 0 && position == getItemCount() - 1) {
            return FOOTERLAYOUT;
        } else {
            return 0;
        }
    }
    class ViewHolderFan extends RecyclerView.ViewHolder{
        CircleImageView mIvFansHeader;
        TextView mTvFansTitle;
        TextView mTvFansId;
        TextView mFanNumTv;
        TextView mFanWorkTv;
        TextView mTvFansFollow;
        public ViewHolderFan(View view) {
            super(view);
            mIvFansHeader = view.findViewById(R.id.iv_fans_header);
            mTvFansTitle = view.findViewById(R.id.tv_fans_title);
            mTvFansId = view.findViewById(R.id.tv_fans_id);
            mFanNumTv = view.findViewById(R.id.fan_num_tv);
            mFanWorkTv = view.findViewById(R.id.fan_work_tv);
            mTvFansFollow = view.findViewById(R.id.tv_fans_follow);
        }
        private void setDataFan(int i){
            mTvFansTitle.setText(listSearch.get(i).getProfileName());
            mFanNumTv.setText(listSearch.get(i).getNumberOfFans()+context.getResources().getString(R.string.fans));
            mFanWorkTv.setText(listSearch.get(i).getNumberOfWorks()+context.getResources().getString(R.string.commonet));
            mTvFansId.setText(context.getResources().getString(R.string.profile_id)+listSearch.get(i).getProfileId());
            ImageUtils.LoadImage(context, listSearch.get(i).getProfileAvatar(), mIvFansHeader);
            if (1 == listSearch.get(i).getIsAttention()){
                mTvFansFollow.setText(context.getResources().getString(R.string.focused));
            }else if (0 == listSearch.get(i).getIsAttention()){
                mTvFansFollow.setText(context.getResources().getString(R.string.focus));
            }else if (2 == listSearch.get(i).getIsAttention()){
                mTvFansFollow.setText(context.getResources().getString(R.string.focus_each));
            }
            mTvFansFollow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.searchFanListener(i,listSearch.get(i));
                }
            });
            mIvFansHeader.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("user_id",listSearch.get(i).getProfileId());
                    bundle.putInt(Constant.TAG, 1);
                    TerminalActivity.showFragment(context, HomeMineFragment.class, bundle);
                }
            });
        }
    }
    class ViewHolder extends  RecyclerView.ViewHolder{
        LinearLayout line;
        LinearLayout lin_foucs_one;
        LinearLayout lin_foucs_two;
        LinearLayout lin_foucs_three;
        LinearLayout lin_foucs_four;
        CircleImageView mFoucsOne;
        TextView mFoucsName;
        TextView mFanNum;
        TextView mFoucsTv;
        CircleImageView mFoucsTwoIv;
        TextView mFoucsTwoName;
        TextView mFanTwoNum;
        TextView mFoucsTwoTv;
        CircleImageView mFoucsThreeIv;
        TextView mFocusThreeName;
        TextView mFanThreeNum;
        TextView mFocusThreeTv;
        CircleImageView mFoucsFourIv;
        TextView mFocusFourName;
        TextView mFanFourNum;
        TextView mFocusFourTv;
        public ViewHolder(View view) {
            super(view);
            mFoucsOne = view.findViewById(R.id.focus_iv);
            mFoucsName = view.findViewById(R.id.focus_name);
            mFanNum = view.findViewById(R.id.fans_num);
            mFoucsTv = view.findViewById(R.id.focus_tv);
            mFoucsTwoIv = view.findViewById(R.id.focus_two_iv);
            mFoucsTwoName = view.findViewById(R.id.focus_two_names);
            mFanTwoNum = view.findViewById(R.id.fans_two_num);
            mFoucsTwoTv = view.findViewById(R.id.focus_two_tv);
            mFoucsThreeIv = view.findViewById(R.id.focus_three_iv);
            mFocusThreeName = view.findViewById(R.id.focus_three_name);
            mFanThreeNum = view.findViewById(R.id.fans_three_num);
            mFocusThreeTv = view.findViewById(R.id.focus_three_tv);
            line=view.findViewById(R.id.line_ll);
            lin_foucs_one=view.findViewById(R.id.lin_foucs_one);
            lin_foucs_two=view.findViewById(R.id.lin_foucs_two);
            lin_foucs_three=view.findViewById(R.id.lin_foucs_three);
            lin_foucs_four = view.findViewById(R.id.lin_foucs_four);
            mFoucsFourIv = view.findViewById(R.id.focus_four_iv);
            mFocusFourName = view.findViewById(R.id.focus_four_name);
            mFanFourNum = view.findViewById(R.id.fans_four_num);
            mFocusFourTv = view.findViewById(R.id.focus_four_tv);
        }
        private void setData(int positon){
            if (positon*4 < listSearch.size()) {
                SearchUserResp atten = listSearch.get(positon * 4);
                lin_foucs_one.setVisibility(View.VISIBLE);
                ImageUtils.LoadImage(context, atten.getProfileAvatar(), mFoucsOne);
                mFoucsName.setText(atten.getProfileName());
                String fans = StringUtils.formatZanCount(Integer.parseInt(atten.getNumberOfFans()));
                mFanNum.setText(fans+context.getResources().getString(R.string.fans));
                if (1 == atten.getIsAttention()){
                    mFoucsTv.setText(context.getResources().getString(R.string.focused));
                }else{
                    mFoucsTv.setText(context.getResources().getString(R.string.focus));
                }
                mFoucsTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        listener.searchFocus(positon * 4,atten);
                    }
                });
                mFoucsOne.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        listener.clickImage(positon*4,atten);
                    }
                });
            }else{
                lin_foucs_one.setVisibility(View.INVISIBLE);
            }
            if (positon*4+1 < listSearch.size()){
                lin_foucs_two.setVisibility(View.VISIBLE);
                SearchUserResp atten = listSearch.get(positon*4+1);
                ImageUtils.LoadImage(context,atten.getProfileAvatar(),mFoucsTwoIv);
                mFoucsTwoName.setText(atten.getProfileName());
                String fans = StringUtils.formatZanCount(Integer.parseInt(atten.getNumberOfFans()));
                mFanTwoNum.setText(fans+context.getResources().getString(R.string.fans));
                if (1 == atten.getIsAttention()){
                    mFoucsTwoTv.setText(context.getResources().getString(R.string.focused));
                }else{
                    mFoucsTwoTv.setText(context.getResources().getString(R.string.focus));
                }
                mFoucsTwoTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        listener.searchFocus(positon*4+1,atten);
                    }
                });
                mFoucsTwoIv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        listener.clickImage(positon*4+1,atten);
                    }
                });
            }else{
                lin_foucs_two.setVisibility(View.INVISIBLE);
            }
            if (positon*4+2< listSearch.size()) {
                SearchUserResp atten = listSearch.get(positon*4+2);
                lin_foucs_three.setVisibility(View.VISIBLE);
                ImageUtils.LoadImage(context,atten.getProfileAvatar(),mFoucsThreeIv);
                mFocusThreeName.setText(atten.getProfileName());
                String fans = StringUtils.formatZanCount(Integer.parseInt(atten.getNumberOfFans()));
                mFanThreeNum.setText(fans+context.getResources().getString(R.string.fans));
                if (1 == atten.getIsAttention()){
                    mFocusThreeTv.setText(context.getResources().getString(R.string.focused));
                }else{
                    mFocusThreeTv.setText(context.getResources().getString(R.string.focus));
                }
                mFocusThreeTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        listener.searchFocus(positon*4+2,atten);
                    }
                });
                mFoucsThreeIv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        listener.clickImage(positon*4+2,atten);
                    }
                });
            }else{
                lin_foucs_three.setVisibility(View.INVISIBLE);
            }
            if (positon*4+3<listSearch.size()){
                SearchUserResp atten = listSearch.get(positon*4+3);
                lin_foucs_four.setVisibility(View.VISIBLE);
                ImageUtils.LoadImage(context,atten.getProfileAvatar(),mFoucsFourIv);
                mFocusFourName.setText(atten.getProfileName());
                String fans = StringUtils.formatZanCount(Integer.parseInt(atten.getNumberOfFans()));
                mFanFourNum.setText(fans+context.getResources().getString(R.string.fans));
                if (1 == atten.getIsAttention()){
                   mFocusFourTv.setText(context.getResources().getString(R.string.focused));
                }else{
                    mFocusFourTv.setText(context.getResources().getString(R.string.focus));
                }
                mFocusFourTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        listener.searchFocus(positon*4+3,atten);
                    }
                });
                mFoucsFourIv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        listener.clickImage(positon*4+3,atten);
                    }
                });
            }else{
                lin_foucs_four.setVisibility(View.INVISIBLE);
            }
        }
    }
    public class FooterHolder extends RecyclerView.ViewHolder {
        View mLoadingViewstubstub;
        View mEndViewstub;
        public FooterState status = FooterState.Normal;

        public FooterHolder(View itemView) {
            super(itemView);
            mLoadingViewstubstub = itemView.findViewById(R.id.glistview_footer_content_rel);
            mEndViewstub = itemView.findViewById(R.id.glistview_footer_nomore_rel);
        }

        //根据传过来的status控制哪个状态可见
        public void setStatus(FooterState status) {
            this.status = status;
            switch (status) {
                case Normal:
                    setAllGone();
                    break;
                case Loading:
                    setAllGone();
                    mLoadingViewstubstub.setVisibility(View.VISIBLE);
                    break;
                case TheEnd:
                    setAllGone();
                    mEndViewstub.setVisibility(View.VISIBLE);
                    break;
                default:
                    break;
            }

        }

        //全部不可见
        void setAllGone() {
            if (mLoadingViewstubstub != null) {
                mLoadingViewstubstub.setVisibility(View.GONE);
            }
            if (mEndViewstub != null) {
                mEndViewstub.setVisibility(View.GONE);
            }
        }

    }
    public interface  FocusSearchListener{
        void searchFocus(int position, SearchUserResp bean);
        void clickImage(int position,SearchUserResp bean);
        void searchFanListener(int position,SearchUserResp listResp);
    }
}
